/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import xml.XMLController;

/**
 *
 * @author bi
 */
public final class CommandDeployment {

    private class CommandDeploymentEach {

        public JComboBox player;
        public JComboBox squad;
        public JTextField amount;

        public CommandDeploymentEach() {
            player = new JComboBox(pList);
            squad = new JComboBox(new String[]{Constant.COMMAND_DEFAULT});
            amount = new JTextField("1");

            ActionListener playerListener = new ActionListener() {

                public void actionPerformed(ActionEvent evt) {

                    if (!list.get(list.size() - 1).player.getSelectedItem().toString().equals(Constant.COMMAND_DEFAULT)) {
                        addCommand();
                    }
                    removeCommand();
                    addSquadCommand((JComboBox) evt.getSource());
                }
            };
            player.addActionListener(playerListener);
        }
    }
    private JPanel parent;
    private XMLController docPlayer, docSquad;
    private Vector<String> pList;
    private Vector<CommandDeploymentEach> list;

    public CommandDeployment(JPanel parent,
            XMLController docPlayer,
            XMLController docSquad) {
        this.parent = parent;
        load(docPlayer, docSquad);
    }

    public void load(XMLController docPlayer,
            XMLController docSquad) {
        pList = new Vector<String>();
        pList.add(Constant.COMMAND_DEFAULT);
        for (int i = 0; i < docPlayer.table.length; i++) {
            if (docPlayer.table[i][3].equals("Human")) {
                pList.add(docPlayer.table[i][0]);
            }
        }

        this.docPlayer = docPlayer;
        this.docSquad = docSquad;

        parent.removeAll();
        list = new Vector<CommandDeploymentEach>();
        addCommand();
    }

    public void addCommand() {
        CommandDeploymentEach cde = new CommandDeploymentEach();
        list.add(cde);
        parent.add(cde.player);
        parent.add(cde.squad);
        parent.add(cde.amount);
        parent.setLayout(new GridLayout(list.size(), 3, 50, 30));
    }

    public void removeCommand() {
        for (int c = 0; c < list.size() - 1; c++) {
            CommandDeploymentEach cde = list.get(c);
            if (list.get(c).player.getSelectedItem().toString().equals(Constant.COMMAND_DEFAULT)) {
                list.remove(cde);
                parent.remove(cde.player);
                parent.remove(cde.squad);
                parent.remove(cde.amount);
                parent.setLayout(new GridLayout(list.size(), 3, 50, 30));
                c--;
            }
        }
    }

    public void addSquadCommand(JComboBox source) {
        for (int c = 0; c < list.size(); c++) {
            CommandDeploymentEach cde = list.get(c);
            if (cde.player.equals(source)) {
                String playerName = cde.player.getSelectedItem().toString();
                String factionName = null;

                for (int i = 0; i < docPlayer.table.length && factionName == null; i++) {
                    if (docPlayer.table[i][0].equals(playerName)) {
                        factionName = docPlayer.table[i][1];
                    }
                }

                Vector<String> squadName = new Vector<String>();
                squadName.add(Constant.COMMAND_DEFAULT);
                for (int i = 0; i < docSquad.table.length; i++) {
                    if (docSquad.table[i][1].equals(factionName)) {
                        if (!squadName.contains(docSquad.table[i][0])) {
                            squadName.add(docSquad.table[i][0]);
                        }
                    }
                }

                cde.squad.setModel(new DefaultComboBoxModel(squadName));
            }
        }
    }

    public String[][] getCommand() {
        String[][] commandList = new String[list.size() - 1][3];
        for (int i = 0; i < list.size() - 1; i++) {
            commandList[i][0] = list.get(i).player.getSelectedItem().toString();
            commandList[i][1] = list.get(i).squad.getSelectedItem().toString();
            commandList[i][2] = list.get(i).amount.getText();
        }
        return commandList;
    }
}
